﻿package fpc.controls.text;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import fpc.styles.skins.ITextBackgroundSkin;
import fpc.styles.skins.NullTextBackgroundSkin;
import fpc.styles.skins.SkinType;
import fpc.styles.Style;
import fpc.utils.ClassFactory;
import fpc.utils.IFactory;
import fpc.utils.Maths;

class TextArea extends Label
{
	/*
	TODO
	
	1. Полосы прокрутки
	2. Включение - отключение заднего фона
	*/
	
	var _activeSkin:ITextBackgroundSkin;
	
	public function new() 
	{
		_activeSkin = Style.findSkin(this, SKIN_TYPE_TEXT_AREA).create();
		
		super();
		
		autoSize = false;
	}
	
	var _sprite:Sprite;
	
	override function createBaseObject():DisplayObject
	{
		super.createBaseObject();
		
		_sprite = new Sprite();
		_sprite.addChild(_activeSkin.object);
		_sprite.addChild(_tf);
		return _sprite;
	}
	
	// PUBLIC METHODS
	
	// END PUBLIC METHODS
	
	// RESIZE
	
	override function updateSize()
	{
		super.updateSize();
		_activeSkin.setSize(Std.int(_width), Std.int(_height));
	}
	
	// END RESIZE
	
	// SKIN
	
	var _skinFactory:IFactory<ITextBackgroundSkin>;
	
	override function updateStyle()
	{
		super.updateStyle();
		
		var skinFactory = Style.findSkin(this, SKIN_TYPE_TEXT_AREA);
		if (skinFactory != _skinFactory)
		{
			_skinFactory = skinFactory;
			applySkin(skinFactory.create());
		}
	}
	
	function applySkin(value:ITextBackgroundSkin)
	{
		if (_activeSkin != null)
		{
			_sprite.removeChild(_activeSkin.object);
		}
		
		_activeSkin = value;
		
		_sprite.addChildAt(value.object, 0);
	}
	
	public static var SKIN_TYPE_TEXT_AREA = new SkinType(
		new ClassFactory<ITextBackgroundSkin>(NullTextBackgroundSkin)
	);
	
	// END SKIN
}